<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Control.php');
require_once($Config->WebCMSRoot.'Models/Form.php');
require_once($Config->WebCMSRoot.'Views/Form.php');
require_once($Config->SiteRoot.'Controls/CustomCode.Form.php');

Class Form extends BaseControl
{
	var $form;
	var $view;
	var $CustomCode;

	function Form($parameters, &$page)
	{
		$this->form = new FormModel();
		$this->view = new FormView();
		$this->CustomCode = new FormCustomCode();

		parent::BaseControl($parameters, &$page);
	}

	function Execute()
	{
//print('<pre>'.var_export($this,true).'</pre>');exit;
		switch (true)
		{
			case ($this->ParameterExists('id')):
				$id = sha1($this->GetParameter('id').'_'.$this->GetParameter('parameters'));
				break;
			case ($this->ParameterExists('name')):
				$id = sha1($this->GetParameter('name'));
				break;
				
			default:
				$valid = false;
		}

		$this->form->Retrieve(&$this->parameters);

		if (count($this->form->Collection) > 0)
		{
			//$this->GetParameter('id') = $this->form->Collection[0]->ID;
			$this->form->Collection[0]->RetrieveFormFields(&$this->parameters);

			if (isset($_POST['formID']) && $_POST['formID'] == $id && empty($_POST['antiLeech']))
			{
				// form is submitted
				$this->ExtractFormData(&$this->form->Collection[0]);

				// Validate user input
				
				eval('$result = $this->CustomCode->'.$this->form->Collection[0]->VerificationFunction.'(&$this);');

				if ($result)
				{
					// Store in database
					if ($this->GetParameter('save') == 'true')
					{
            $this->form->PersistEntries($_POST['formCode']);
					}
					
					// Show feedback to user
					$this->view->ShowFeedback($this->form->Collection[0]);

				  $this->form->Collection[0]->IsComplete = true;
//print('<pre>'.var_export($this->form->Collection[0],true).'</pre>');exit;
				}
				else
				{
				  $this->form->Collection[0]->IsComplete = false;

					// Show errors to user
					$this->view->ShowError($this->form->Collection[0]);
					$output = $this->view->output;
					
					// Show form again
					$this->view->ShowForm(&$this->form, $id);
					$this->view->output = $output . $this->view->output;
//print(htmlspecialchars($this->view->output));exit;
				}

				$_SESSION[$id] = $this->form->Collection[0]; // store for future usage
			}
			else
			{
				// new form
				$this->view->ShowForm(&$this->form, $id);
			}
		}
		else
		{
			// Form does not exist
		}

		parent::Execute();
	}

	function ValidateArguments()
	{
//print('<pre>'.var_export($this->parameters,true).'</pre>');exit;
		$valid = (count($this->parameters) >= 1);
		
		switch (true)
		{
			case ($this->ParameterExists('id')):
				$valid = ($valid && $this->ParameterExists('parameters'));
				break;
			case ($this->ParameterExists('name')):
				break;
				
			default:
				$valid = false;
		}

		return $valid;
	}
	
	// Extended functions
	
	function ExtractFormData(&$form)
	{
		reset($form->FormFields->Collection);
		while (list($key) = each($form->FormFields->Collection))
		{
			$formField =& $form->FormFields->Collection[$key];
			if (isset($_POST['ff_'.$form->ID.'_'.$formField->Name]))
			{
			  $formField->Value = $_POST['ff_'.$form->ID.'_'.$formField->Name];
			}
		}
	}
	
	function GetFormField($name)
	{
		foreach ($this->form->Collection[0]->FormFields->Collection as $key=>$formField)
		{
			if ($formField->Name == $name)
			{
				return $formField;
			}
		}
		
		return null;
	}
}
?>